<style lang="less">
    @import '../../styles/loading.less';
</style>

<template>
    <div>
        <Col span="12">
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> {{comment_id?'编辑':'新增'}}评论
                </p>
                <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                    <Form-item label="订单ID" prop="order_id">
                        <router-link :to="{name: 'orderPage', query: {order_id: formValidate.order_id}}">{{formValidate.order_id}}</router-link>
                    </Form-item>
                    <Form-item label="用户名" prop="username">
                        {{formValidate.username}}
                    </Form-item>
                    <Form-item label="评价" prop="is_online">
                        <Radio-group v-model="formValidate.level">
                            {{formValidate.level==1?'差评':(formValidate.level==2?'中评':'好评')}}
                        </Radio-group>
                    </Form-item>
                    <Form-item label="评论内容" prop="content">
                        {{formValidate.content}}
                    </Form-item>
                    <Form-item label="回复" prop="reply">
                        <Input type="textarea" v-model="formValidate.reply" placeholder="Enter..."></Input>
                    </Form-item>

                    <Form-item>
                        <Button type="primary" v-if="noReply" @click="handleSubmit('formValidate')">提交</Button>
                    </Form-item>
                </Form>
            </Card>
        </Col>
    </div>
</template>

<script>
    export default {
        components:{

        },
        data () {
            return {
                comment_id: '',
                noReply: true,
                formValidate: {
                    order_id: 0,
                    username: '',
                    level: 3,
                    content: '',
                    reply: '',
                },
                ruleValidate: {
                    reply: [
                        { required: true, message: '请填写回复' },
                    ],
                },
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let postData = {reply: this.formValidate.reply};
                        postData.comment_id = this.comment_id;
                        this.$http.post('admin/goods/replyComment', this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                this.$router.push({
                                    name: 'commentList'
                                });
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            getOne () {
                this.$http.post('admin/goods/getOneComment', this.$qs.stringify({comment_id: this.comment_id})).then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.formValidate = {
                            order_id: d.data.order_id,
                            username: d.data.username,
                            level: d.data.level,
                            content: d.data.content,
                            reply: d.data.reply,
                        };
                        this.noReply = d.data.reply=='';
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            },
        },
        mounted() {
            this.$nextTick(() => {
                this.comment_id = this.$route.query.comment_id;
                if (this.comment_id) {
                    this.getOne();
                }
            });
        },
    };
</script>
